<?php defined('SYSPATH') OR die('No direct access allowed.');

class Template_Plugin extends WB_FormItem {
	
	protected $template, $func;
	
	public function __construct($name='', $params=array())
	{
		parent::__construct($name, $params);
		
		$this->table_columns = isset($params['table_columns']) ? $params['table_columns'] : NULL;
		$this->form_columns = NULL;
		$this->db_columns = NULL;
        
        $this->template = isset($this->params['template']) ? $this->params['template'] : '';
		$this->func = isset($this->params['func']) ? $this->params['func'] : NULL;
	}
	
	public function prep_db() {return NULL;}
	public function render_item() {return NULL;}
	
	public function render_cell()
	{
		if ($this->func)
		{
			$func = $this->func;
			$template = Kohana::instance()->$func($this->value);
		}
		else
		{
			$template = $this->template;
		}
			
		$search = array('{'.$this->name.'}' => 'NULL');
		if (is_array($this->value))	foreach ($this->value as $k=>$v) $search['{'.$k.'}'] = $v;
		elseif ( ! empty($this->value)) $search = array('{'.$this->name.'}' => $this->value);
		$search['{base_url}'] = url::base();
		$search['{site_url}'] = url::site();
		$template = str_replace(array_keys($search), $search, $template);
		
		return $template;
	}
}

//End of Template_Plugin Library